package com.zksk.admin.controller.member;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.api.domain.SysDictData;
import com.zksk.admin.domain.member.dto.DistributionDto;
import com.zksk.admin.domain.member.dto.OperatorDto;
import com.zksk.admin.domain.member.dto.MemberDto;
import com.zksk.admin.domain.member.vo.MemberVo;
import com.zksk.admin.service.member.MemberService;
import com.zksk.admin.service.system.ISysDictTypeService;
import com.zksk.common.core.constant.AdminConstants;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.mbg.admin.domain.MemberManage;
import com.zksk.mbg.admin.domain.MemberOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther xiebo
 * @date 2022/12/14
 **/
@Slf4j
@RestController
@RequestMapping("/member/")
@Api(tags = "企业管理相关接口")
public class MemberController extends BaseController
{

    @Autowired
    private MemberService memberService;

    @Autowired
    private ISysDictTypeService dictTypeService;


    @GetMapping("getById/{id}")
    @Log(title = "查询企业信息", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据ID查询企业信息")
    public R<MemberVo> getById(@PathVariable Long id)
    {
        return memberService.selectById(id);
    }

    @PostMapping("selectList")
    @Log(title = "查询企业信息", businessType = BusinessType.SELECT)
    @ApiOperation(value = "分页条件查询企业信息")
    public R<Page<MemberVo>> selectList(@RequestBody MemberDto memberDto)
    {
        return memberService.selectList(memberDto);
    }

    @DeleteMapping("deleteOperator/{ids}")
    @Log(title = "删除经办人", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除经办人")
    public R deleteOperator(@PathVariable("ids") List<Long> ids)
    {
        return memberService.deleteOperator(ids);
    }

    @PostMapping("getManageList")
    @Log(title = "查询企业信息", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查询历史经办人")
    public R<Page<MemberOperator>> getManageList(@RequestBody OperatorDto operatorDto)
    {
        if (operatorDto.getMemberId() == null || operatorDto.getMemberId().equals(0L))
        {
            return R.fail("未传企业ID");
        }
        return memberService.getManageList(operatorDto);
    }

    @GetMapping("selectMemberType")
    @Log(title = "查询企业类型", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查询企业类型-数据字典")
    public R<List<SysDictData>> selectMemberType()
    {
        return R.ok(dictTypeService.selectDictDataByType(AdminConstants.MEMBER_TYPE));
    }

    @GetMapping("selectTradeType")
    @Log(title = "查询行业类型", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查询行业类型-数据字典")
    public R<List<SysDictData>> selectTradeType()
    {
        return R.ok(dictTypeService.selectDictDataByType(AdminConstants.TRADE_TYPE));
    }

    @PutMapping("distribution")
    @Log(title = "分配营销人员", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "分配营销人员")
    public R distribution(@RequestBody @Validated(Update.class) DistributionDto distributionDto)
    {
        if (CollUtil.isEmpty(distributionDto.getIds()))
        {
            return R.fail("未选择企业!");
        }
        return memberService.distribution(distributionDto);
    }

    @PostMapping("add")
    @Log(title = "新增企业信息", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增企业信息")
    public R add(@RequestBody @Validated(Insert.class) MemberVo memberVo)
    {
        if (memberService.checkMemberName(memberVo.getMemberName()))
        {
            return R.fail("企业名称已存在！");
        }
        if (memberService.checkCreditCode(memberVo.getCreditCode()))
        {
            return R.fail("企业信用代码已存在！");
        }
        return memberService.insert(memberVo);
    }

    @PutMapping("update")
    @Log(title = "修改企业信息", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改企业信息")
    public R update(@RequestBody @Validated(Update.class) MemberVo memberVo)
    {
        return memberService.updateMember(memberVo);
    }

    @DeleteMapping("delete/{ids}")
    @Log(title = "删除企业信息", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除企业信息,逻辑删除")
    public R delete(@PathVariable("ids") List<Long> ids)
    {
        return memberService.deleteMember(ids);
    }


}
